
#include "hashMap.h"
//初始化
HashMap* Init(){
    HashMap * hashMap = (HashMap*) malloc(sizeof(HashMap));
    hashMap->curSz = 0;
    hashMap->max = INITIALMAX;

    Node** nodes = (Node**) malloc(sizeof(Node*) * INITIALMAX);
    memset(nodes,0,sizeof(Node*) * INITIALMAX);
    hashMap->nodes = nodes;
    return hashMap;
}
int IsEmpty(HashMap * hashMap){
    return hashMap->curSz == 0;
}
//输出
void Print(HashMap * hashMap){
    if(!IsEmpty(hashMap)){
        Node** nodes = hashMap->nodes;
        for(int i = 0; i < hashMap->max;i++){
            Node* cur = nodes[i];
            if(cur){
                while(cur){
                    printf("%d:%d->",cur->key,cur->value);
                    cur = cur->next;
                }
                printf("NULL\n");
            }

        }
    }
}
//映射
int Hash(HashMap* hashMap, void* key){
    return ((int)key) % hashMap->max;
}
//存放
void Put(HashMap* hashMap,void* key, void* value){
    Node* node = (Node*) malloc(sizeof(Node));
    node->key = key;
    node->value = value;
    node->next = NULL;
    //定位
    int index = Hash(hashMap, key);
    if(hashMap->nodes[index] == NULL){  //桶中没有东西
        hashMap->nodes[index]= node;
        hashMap->curSz++;
    } else {
        Node* cur = hashMap->nodes[index];
        //往尾部差
        if(cur) {
            while(cur->next){
                if(node->key == cur->key){  //相同key，覆盖
                    cur->value = value;
                    free(node);
                    hashMap->curSz++;
                    return;
                }
                cur = cur->next;
            }
            cur->next = node;
            hashMap->curSz++;
        }
    }
}
//获取
void * Get(HashMap * hashMap,void* key){
    Node ** nodes = hashMap->nodes;
    int index = Hash(hashMap,key);

    Node* cur = nodes[index];
    while(cur){
        if(cur->key == key){
            return cur->value;
        }
        cur = cur->next;
    }
    return NULL;
}
//删除
void Delete(HashMap * hashMap, void* key){
    Node ** nodes = hashMap->nodes;
    int index = Hash(hashMap,key);
    Node* cur = nodes[index];
    Node* prev = NULL;
    while(cur){
        if(cur->key == key){
            if(prev && cur->next){
                prev ->next = cur->next;
                free(cur);
                break;
            }  else if(prev == NULL && cur->next){
                Node * next = cur->next;
                nodes[index] = next;
                free(cur);
                break;
            } else if(prev && cur ->next == NULL) {
                free(cur);
                prev->next = NULL;
                break;
            }
        }
        prev = cur;
        cur = cur->next;
    }
}
//清除全部
void Clear(HashMap * hashMap){
    Node ** nodes = hashMap->nodes;
    for(int i = 0; i < hashMap->max; i++){
        Node * cur = nodes[i];
        Node * prev = NULL;
        nodes[i] = NULL;
        while(cur) {
            prev = cur;
            cur = cur->next;
            free(prev);
            prev = NULL;
        }
    }
    hashMap->curSz = 0;
}
int ContainKey(HashMap* hashMap, void* key){
    Node ** nodes = hashMap->nodes;
    int index = Hash(hashMap,key);
    Node* cur = nodes[index];

    while(cur){
        if(cur->key == key){
            return 1;
        }
        cur = cur->next;
    }
    return 0;
}